from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from .models import Invitations, UserInfo, RenameCard, MemeQuestions
from django.urls import reverse
from .forms import LoginForm, RegisterForm, RenameForm
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from student.models import Student
from fuzzywuzzy import fuzz

def logins(request):
	if request.method == 'POST':
		login_form = LoginForm(request.POST)
		user_name = request.POST['name']
		user_password = request.POST['password']
		user = authenticate(username=user_name, password=user_password)
		if user == None:
			return render(request, "login/login.html", context={'login_form': login_form, 'errmsg': '用户名不存在或密码错误'})
		else:
			login(request, user)
			return HttpResponseRedirect(reverse("home:index"))
	else:
		login_form = LoginForm()
		context = {"login_form":login_form}
		return render(request, "login/login.html", context=context)

def logouts(request):
	logout(request)
	return HttpResponseRedirect(reverse("home:index"))

def register(request, meme_id=0):
	if request.method == 'POST':
		meme = MemeQuestions.objects.filter(id=meme_id).first()
		register_form = RegisterForm()
		user_name = request.POST['name']
		true_name = request.POST['true_name']
		user_password = request.POST['password']
		user_rep_password = request.POST['repeat_password']
		user_code = request.POST['code']
		meme_answer = request.POST['meme_answer']

		if user_password != user_rep_password:
			return render(request, "login/register.html", context={'errmsg':"密码不一致", 'register_form':register_form})
		if len(user_password) < 6:
			return render(request, "login/register.html", context={'errmsg':"密码长度需长于6位", 'register_form':register_form})
		if User.objects.filter(username=user_name):
			return render(request, "login/register.html", context={'errmsg':'该用户已存在', 'register_form':register_form})
		#if meme_answer.strip().encode('utf-8') != register_form.meme.answer.strip().encode('utf-8'):
		#	return render(request, "login/register.html", context={'errmsg':'你貌似想错了什么', 'register_form':register_form})
		if fuzz.partial_ratio(meme_answer.encode('utf-8'), meme.answer.encode('utf-8')) <= 80:
			return render(request, "login/register.html", context={'errmsg':'你貌似想错了什么', 'register_form':register_form})

		if not Invitations.objects.filter(code=user_code):
			return render(request, "login/register.html", context={"errmsg":"邀请码不存在", 'register_form':register_form})
		code_class = get_object_or_404(Invitations, code=user_code)
		#if code_class.itsname.strip().encode('utf-8') != true_name.strip().encode('utf-8'):
		#	return render(request, "login/register.html", context={"errmsg":"这个邀请码貌似和你不匹配", 'register_form':register_form})
		if fuzz.partial_ratio(true_name.encode('utf-8'), code_class.itsname.encode('utf-8')) <= 80:
			return render(request, "login/register.html", context={'errmsg':'这个邀请码貌似和你不匹配', 'register_form':register_form})
		if code_class.cs <= 0:
			return render(request, "login/register.html", context={'errmsg':'邀请码已失效', 'register_form':register_form})
		code_class.cs -= 1
		code_class.save()
		new_userinfo = UserInfo(theme="default")
		new_userinfo.save()
		user = User.objects.create_user(user_name, user_name + '@qq.com', user_password, userinfo=new_userinfo)
		user.save()
		users = authenticate(username=user_name, password=user_password)
		Student.objects.create(name=true_name, seat_number=Student.objects.all().count()+1, user=user)
		login(request, users)
		return HttpResponseRedirect(reverse("home:index"))
	else:
		register_form = RegisterForm()
		next_url = request.GET.get('next', '')
		context = {'register_form': register_form, 'next_url': next_url}
		return render(request, 'login/register.html', context=context)

def rename(request):
	if request.method == 'POST':
		rename_form = RenameForm()
		user_last_name = request.POST['now_name']
		user_password = request.POST['password']
		user_new_name = request.POST['new_name']
		rename_card_code = request.POST['code']

		users = authenticate(username=user_last_name, password=user_password)
		if users == None:
			return render(request, 'login/rename.html', context={'rename_form': rename_form, 'errmsg': '用户不存在或密码错误'})

		if not RenameCard.objects.filter(code=rename_card_code):
			return render(request, 'login/rename.html', context={'rename_form': rename_form, 'errmsg': '改名卡不存在'})

		rename_card = get_object_or_404(RenameCard, code=rename_card_code)
		if rename_card.number_for_times <= 0:
			return render(request, 'login/rename.html', context={'rename_form': rename_form, 'errmsg': '改名卡已失效'})

		rename_card.number_for_times -= 1
		rename_card.save()

		users.username = user_new_name
		users.save()

		return HttpResponseRedirect(reverse("home:index"))
	else:
		rename_form = RenameForm()
		context = {'rename_form': rename_form}
		return render(request, 'login/rename.html', context=context)
